﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DVDFilmoteka.Forms;
using MovieStorage;
using DVDFilmoteka.src;

namespace DVDFilmoteka.Forms
{
    public partial class ImportForm : LocalizableForm
    {
        private readonly MainForm parrentForm;
        private readonly MovieDataStore12 defaultDataStore = new MovieDataStore12();

        public ImportForm(MainForm parrentForm, MovieDataStore12 defaultDataStore)
        {
            InitializeComponent();
            this.parrentForm = parrentForm;
            this.defaultDataStore = defaultDataStore;
        }

        private void MultiOpenForm_Load(object sender, EventArgs e)
        {
            comboBoxTemplate.Items.AddRange(TemplateManager.Instance.AvailableTemplateNames.ToArray());
            int i = 0;
            foreach (Object item in comboBoxTemplate.Items)
            {
                if (item is String)
                {
                    if ("default".Equals(item as String))
                    {
                        comboBoxTemplate.SelectedIndex = i;
                        break;
                    }
                }
                i++;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        // Pridat
        private void button2_Click(object sender, EventArgs e)
        {
            MediaTemplate template = TemplateManager.Instance.GetTemplateByName("default");
            try
            {
                template = TemplateManager.Instance.GetTemplateByName(comboBoxTemplate.SelectedItem as String);
            }
            catch { }

            List<MovieDataStore12> filmList = new List<MovieDataStore12>();

            foreach (var item in textBox1.Lines)
            {
                if (String.IsNullOrEmpty(item.Trim())) continue;

                MovieDataStore12 dataStore = (MovieDataStore12)defaultDataStore.Clone();
                if (radioButtonCzechNames.Checked)
                {
                    dataStore.czechName = item.Trim();
                }
                else
                {
                    dataStore.originalName = item.Trim();
                }
                dataStore.timeOfAddtion = DateTime.Now;
                dataStore.compression = template.compression;
                dataStore.container = template.container;
                dataStore.channelCount = template.channelCount;
                dataStore.language = template.language;
                dataStore.medium = template.medium;
                dataStore.quality = template.quality;
                dataStore.resolution = template.resolution;
                dataStore.size = template.size;

                filmList.Add(dataStore);
            }

            parrentForm.addFilmDataStore(filmList);
            Close();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            radioButtonOriginalNames.Checked = !radioButtonCzechNames.Checked;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            radioButtonCzechNames.Checked = !radioButtonOriginalNames.Checked;
        }

        public override void Localize()
        {
            this.Text = Properties.Messages.form_import_text;
            buttonCancel.Text = Properties.Messages.form_import_buttonCancel;
            buttonImport.Text = Properties.Messages.form_import_buttonImport;
            labelImportAs.Text = Properties.Messages.form_import_labelImportAs;
            labelTemplate.Text = Properties.Messages.form_import_labelTemplate;
            radioButtonCzechNames.Text = Properties.Messages.form_import_radioButtonCzechNames;
            radioButtonOriginalNames.Text = Properties.Messages.form_import_radioButtonOriginalNames;
        }
    }
}